<?php

namespace Api\Logic;

use Hyperf\Contract\LengthAwarePaginatorInterface;

class InfoLogic
{

    public function getClass($params): array
    {
        $query = \App\Info\Model\InfoClass::query()
            ->select(['id', 'name'])
            ->where('status', 1);
        $query->orderBy('display_order','desc');
        $list = $query->get();
        return $list->isEmpty()?[]:$list->toArray();
    }

    public function getList($params): array
    {
        $query = \App\Info\Model\InfoItem::query()
            ->select(['id','title','author','summary','class_id','thumb','display_order','is_recommend','create_time'])
            ->where('status', 1);
        if(!empty($params['class_id'])){
            $query->where('class_id', $params['class_id']);
        }
        if(!empty($params['is_recommend'])){
            $query->where('is_recommend', $params['is_recommend']);
        }
        $query->with(['class'=>function($query){
            $query->select(['id','name']);
        }]);
        $query->orderBy('create_time','desc');
        $paginate = $query->paginate(
            (int)($params['pageSize'] ?? 10),
            ['*'],
            'page',
            (int)($params['page'] ?? 1)
        );
        return $this->setPaginate($paginate);
    }
    public function getDetail($params): array
    {
        $query = \App\Info\Model\InfoItem::query()
            ->select(['id','title','author','summary','class_id','thumb','display_order','is_recommend','content','create_time'])
            ->where('status', 1);
        if(!empty($params['id'])){
            $query->where('id', $params['id']);
        }
        $query->with(['class'=>function($query){
            $query->select(['id','name']);
        }]);
        $info=$query->first();
        return empty($info)?[]:$info->toArray();
    }
    public function getRecruitmentList($params){
        $query = \App\Info\Model\InfoRecruitment::query()
            ->select(['id', 'name', 'ask_require', 'salary_benefits', 'addr', 'remark'])
            ->where('status', 1);
        $query->orderBy('display_order','desc');
        $list = $query->paginate(
            (int)($params['pageSize'] ?? 10),
            ['*'],
            'page',
            (int)($params['page'] ?? 1)
        );
        return $this->setPaginate($list);
    }

    public function getCompany($params){
        $settingConfig = \App\Setting\Model\SettingConfig::query()
            ->whereIn('key', [
                'company_addr',
                'company_addr_img',
                'company_email',
                'company_en_name',
                'company_name',
           
                'company_tel',
                'code_body'
            ])
            ->pluck('value', 'key');
            $settingConfig['code_body']=empty($settingConfig['code_body'])?[]:json_decode($settingConfig['code_body'],true);
        return  $settingConfig;
    }
    public function cultureConfig($params){
        $settingConfig = \App\Setting\Model\SettingConfig::query()
            ->whereIn('key', [
                'culture_title',
                'culture_title_img',
                'culture_desc',
                'culture_icon',
                'culture_right_img'
            ])
            ->pluck('value', 'key');
        return  $settingConfig;
    }

    public function getProduct($params){
        $res= \App\Info\Model\InfoProduct::query()->where('category_key',$params['key'])->first();
        return $res;
    }

    public function getAdv($params){
       $res=\App\Info\Model\InfoAdvPosition::query()
           ->where('key',$params['key'])
           ->with(['adv'=>function ($query){
//               $query->where('adv_start_time','<',date('Y-m-d H:i:s'));
//               $query->where('adv_end_time','>',date('Y-m-d H:i:s'));
               $query->orderBy('order_sort','asc');
           }])
           ->first();
       return $res;
    }

    public function getSingle($params){
        $res=\App\Info\Model\InfoSingle::query()
            ->where('key',$params['key'])->first();
        return $res;
    }

    public function getCulture($params){
      return  \App\Info\Model\InfoCompany::query()->where('id',1)->first();
    }

    public function getBrand($params){
        return  \App\Info\Model\InfoCompany::query()
            ->select(['id','title','img_title','sub_title','summary','thumb','one_img','two_img'])
            ->where('id',2)->first();
    }


    public function setPaginate(LengthAwarePaginatorInterface $paginate): array
    {
        return [
            'items'    => $paginate->items(),
            'pageInfo' => [
                'total'       => $paginate->total(),
                'currentPage' => $paginate->currentPage(),
                'totalPage'   => $paginate->lastPage()
            ]
        ];
    }
    
}